﻿using System;

namespace Xenta.Utils
{
    /// <summary>
    /// Represents the MIME type helper.
    /// </summary>
    public static class MimeTypeHelper
    {
        #region Methods

        /// <summary>
        /// Determins if the MIME type is an image MIME type.
        /// </summary>
        /// <param name="s">The MIME type.</param>
        /// <returns>true if the MIME type is the image MIME type; otherwise false.</returns>
        public static bool IsImage(string s)
        {
            return !String.IsNullOrWhiteSpace(s) && s.StartsWith("image", StringComparison.InvariantCultureIgnoreCase);
        }

        /// <summary>
        /// Gets the file extensions by a MIME type.
        /// </summary>
        /// <param name="s">The MIME type.</param>
        /// <returns>The file extension</returns>
        public static string TypeToExt(string s)
        {
            s = StringHelper.EnsureNotNull(s)
                .Trim()
                .ToLowerInvariant();
            switch(s)
            {
                case "image/gif":
                    return "gif";
                case "image/jpeg":
                    return "jpeg";
                case "image/jpg":
                    return "jpg";
                case "image/png":
                    return "png";
                case "text/plain":
                    return "txt";
                case "application/pdf":
                case "text/pdf":
                    return "pdf";
                case "text/html":
                    return "html";
                case "text/xml":
                    return "xml";
                case "text/csv":
                case "application/csv":
                case "text/comma-separated-values":
                    return "csv";
                case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
                    return "xlsx";
                case "application/vnd.ms-excel":
                    return "xls";
                case "application/gzip":
                    return "gz";
            }
            return String.Empty;
        }

        /// <summary>
        /// Gets the MIME type by a file extension.
        /// </summary>
        /// <param name="s">The file extension.</param>
        /// <returns>The MIME type or an empty string.</returns>
        public static string ExtToType(string s)
        {
            s = StringHelper.EnsureNotNull(s)
                .Trim().TrimStart('.')
                .ToLowerInvariant();
            switch(s)
            {
                case "gif":
                    return "image/gif";
                case "jpeg":
                    return "image/jpeg";
                case "jpg":
                    return "image/jpg";
                case "png":
                    return "image/png";
                case "txt":
                    return "text/plain";
                case "pdf":
                    return "application/pdf";
                case "html":
                case "htm":
                    return "text/html";
                case "xml":
                    return "text/xml";
                case "csv":
                    return "text/csv";
                case "xls":
                    return "application/vnd.ms-excel";
                case "xlsx":
                    return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                case "gz":
                    return "application/gzip";
            }
            return String.Empty;
        }

        #endregion
    }
}
